package com.example.demo.DBServer.thisDatabase.dao;

import com.example.demo.entity.ExecuteConditionEntity;
import com.example.demo.entity.ExecuteInformationEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ExecuteConditionDao {
    /**
     * 新增执行条件
     */
    @Insert({"<script>"
                + "insert into t_execute_condition("
                + "execute_information_id, parent_condition_id, "
                + "connect_type, condition_details) "
                + "values(#{condition.executeInformationId}, #{condition.parentConditionId}, "
                + "#{condition.connectType}, #{condition.conditionDetails})"
            + "</script>"
    })
    @Options(useGeneratedKeys = true, keyProperty = "condition.id", keyColumn = "id")
    Integer saveExecuteCondition(@Param("condition") ExecuteConditionEntity executeCondition);

    /**
     * 批量插入执行条件
     */
    @Insert({"<script>"
                + "INSERT INTO `t_execute_condition` "
                + "(execute_information_id, parent_condition_id, "
                + "connect_type, condition_details)"
                + "VALUES "
                    + "<foreach collection='list' item='item' index='index' separator=','>"
                        + "(#{item.executeInformationId}, #{item.parentConditionId},"
                        + " #{item.connectType}, #{item.conditionDetails}) "
                    + "</foreach>"
            + "</script>"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer batchSaveExecuteCondition(@Param("list") List<ExecuteConditionEntity> configs);

    /**
     * 根据id查询一个执行信息实体
     */
    @Select(value = {"select * from t_execute_condition where id = #{id} "})
    @Results(id = "executeConditionMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "execute_information_id", property = "executeInformationId"),
            @Result(column = "parent_condition_id", property = "parentConditionId"),
            @Result(column = "connect_type", property = "connectType"),
            @Result(column = "condition_details", property = "conditionDetails")
    })
    ExecuteConditionEntity findOneById(@Param("id") Long id);

    /**
     * 根据执行信息id查询
     */
    @Select(value = {"select * from t_execute_condition where execute_information_id = #{executeInformationId} "})
    @ResultMap("executeConditionMap")
    List<ExecuteConditionEntity> findAllByExecuteInformationId(@Param("executeInformationId")Long executeInformationId);

}
